//
//  UIView+ZCHelp.m
//  UniversalApp
//
//  Created by 凯朱 on 2019/5/31.
//  Copyright © 2019 徐阳. All rights reserved.
//

#import "UIView+ZCHelp.h"

@implementation UIView (ZCHelp)
/**
 *  自动从xib创建视图
 */
+(instancetype)viewFromXIB{
    
    NSString *name = NSStringFromClass(self);
    
    UIView *xibView = [[[NSBundle mainBundle] loadNibNamed:name owner:nil options:nil] firstObject];
    
    return xibView;
}

-(void)setTextFieldPlaceholdarWithStr:(NSString *)placeholdar textColor:(UIColor *)textColor font:(UIFont *)font{
    UITextField *textField = (UITextField *)self;
    if (font == nil) {
        font = kFont(18);
    }
    if (textColor == nil) {
        textColor = KColor_4;
    }
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:kSTRING(placeholdar) attributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:textColor}];
}

//添加横向渐变色
-(CAGradientLayer *)addLandColorWithStart:(UIColor *)startColor endColor:(UIColor *)endColor frame:(CGRect)frame{
    // 创建一个 CAGradientLayer 对象
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    // 设置渐变层的框架大小为 UIView 的大小
    gradientLayer.frame = frame;
    // 设置渐变的起始和结束颜色
    gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
    // 设置渐变的方向为从左到右
    gradientLayer.startPoint = CGPointMake(0.0, 0.5);
    gradientLayer.endPoint = CGPointMake(1.0, 0.5);
    // 将渐变层添加到 UIView 的图层上
    [self.layer insertSublayer:gradientLayer atIndex:0];
    return gradientLayer;
}

//添加竖向渐变色
-(void)addPortColorWithStart:(UIColor *)startColor endColor:(UIColor *)endColor frame:(CGRect)frame{
    // 创建一个 CAGradientLayer 对象
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    // 设置渐变层的框架大小为 UIView 的大小
    gradientLayer.frame = frame;
    // 设置渐变的起始和结束颜色
    gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
    // 设置渐变的方向为从上到下
    gradientLayer.startPoint = CGPointMake(0.5, 0.0);
    gradientLayer.endPoint = CGPointMake(0.5, 1.0);
    // 将渐变层添加到 UIView 的图层上
    [self.layer insertSublayer:gradientLayer atIndex:0];
    
}

- (void)setRoundedCorners:(CGFloat)radius forCorners:(UIRectCorner)corners frame:(CGRect)frame {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:frame
                                                   byRoundingCorners:corners
                                                         cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

-(void)removeWithAnimation{
    [UIView animateWithDuration:0.25 animations:^{
        self.mj_y = kScreenHeight;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)showWithAnmation:(UIView *)onView{
    if ([onView viewWithTag:1001111]) {
        return;
    }
    self.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight);
    self.tag = 1001111;
    [onView addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        self.mj_y = 0;
    } completion:^(BOOL finished) {
        
    }];
}

@end
